package com.qiyee.dalligent.common.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.*;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.Data;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * Title: EasyExcelBarcodeExample
 * Description:
 *
 * @author xt.ahs.shiyaning1
 * @version 1.0.0
 * @Date 2025/1/8 09:34
 */
public class EasyExcelBarcodeExample {

    public static void main(String[] args) throws IOException, WriterException {
        List list = getList();
        CustomCellWriteHandler customCellWriteHandler = new CustomCellWriteHandler();
        String fileName = new Random().nextInt(100)+ "barcode.xlsx";
        LoopMergeStrategy loopMergeStrategy =new LoopMergeStrategy(3,1,1);
        LoopMergeStrategy loopMergeStrategy2 =new LoopMergeStrategy(3,1,0);
        EasyExcel.write(fileName, User.class)
            .registerWriteHandler(customCellWriteHandler)
            .registerWriteHandler(loopMergeStrategy2)
            //.registerWriteHandler(loopMergeStrategy)
            .registerWriteHandler(writeCenterStyle())
            // .excludeColumnFiledNames(Arrays.asList("code"))
            .sheet("Sheet1")
            .doWrite(list); // 假设data()是提供数据的方法

        // 读取文件并显示
        System.out.println("Barcode file created at: " + fileName);
    }
    public static HorizontalCellStyleStrategy writeCenterStyle(){
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 自动换行
        contentWriteCellStyle.setWrapped(true);
        //设置 垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        //设置边框样式
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);//细实线
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);

        WriteFont bodyWriteFont = new WriteFont();
        bodyWriteFont.setFontHeightInPoints((short)12);				// 设置字体大小为20
        //headWriteFont.setItalic(BooleanEnum.TRUE.getBooleanValue());// 设置字体斜体
        contentWriteCellStyle.setWriteFont(bodyWriteFont);
        //contentWriteCellStyle
        // 头部
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 设置字体居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        WriteFont headWriteFont = new WriteFont();
        // 设置字体大小为20
        headWriteFont.setFontHeightInPoints((short)16);
        //headWriteFont.setItalic(BooleanEnum.TRUE.getBooleanValue());// 设置字体斜体
        headWriteCellStyle.setWriteFont(headWriteFont);

        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }

    private static List getList() {
        List list= new ArrayList();
        for (int i = 0; i < 10; i++) {
            // 创建条形码
            String text = "123456789012"+i; // 条形码内容



            // 使用EasyExcel写入图片到单元格

            // CustomCellWriteHandler customCellWriteHandler = new CustomCellWriteHandler(1, 0, "新内容");
            User user =new User();
            StringBuilder orderInfoBuild = new StringBuilder();
            orderInfoBuild.append("Apple 苹果 iPhone 5 国行 香槟金色 64G 全网通");
            orderInfoBuild.append("\n");
            orderInfoBuild.append("质检编码：");
            orderInfoBuild.append("99689070885888");
            orderInfoBuild.append("\n");

            orderInfoBuild.append("渠道订单号：304455581040");
            orderInfoBuild.append("\n");
            orderInfoBuild.append("IMEI/SN(唯一码)：zx20250109171011752");
            orderInfoBuild.append("\n");

            orderInfoBuild.append("状态：待发货");
            user.setName(orderInfoBuild.toString());
            user.setBarcode(text);

            User user1 = new User();
            user1.setName(orderInfoBuild.toString());
            list.add(user1);
            list.add(user);
            list.add(user1);
        }



        return list;
    }


}
@ContentRowHeight(31)
@Data
@HeadFontStyle(fontHeightInPoints = 16)
@ContentStyle(
    fillPatternType = FillPatternType.SOLID_FOREGROUND,
    fillForegroundColor = 1,
    wrapped = true
  )
class User {
    @ExcelProperty(value = "订单信息",index = 0)
    @ColumnWidth(50)
    private String name;
    @ColumnWidth(30)

    private String barcode;
}

